// setupWebOpenApp.js
//初始化网页跳转app工具
var currentCid = null;
var currentTrainCid = null;
var currentTrainCateid = null;
function setupLoginSucceedNotifi(){
	api.addEventListener({
	    name: 'loginSucceedNotifi'
	}, function(ret, err) {
	    setupLoginSucceedShareCourse();
	});
}
function setupLoginSucceedShareCourse(){
	// if ($api.getStorage('cfnetShareCourseStatus') == 0) {//如果未打开
	// 	var shareTime = parseInt($api.getStorage('cfnetShareCourseTime'));
	// 	if(getCurrentTime10() - shareTime < 120){//如果分享课程未过期
			if(currentCid != null && currentCid != 'undefined'){
				setTimeout(function(){//3秒后执行是否跳转课程
					jumpToCourse(currentCid);
					//清空跳转cid
					currentCid = null;
			    }, 3000);
			}
			if(currentTrainCid != null && currentTrainCid != 'undefined'){
				setTimeout(function(){//3秒后执行是否跳转课程
					if (currentTrainCateid == 1) {//课程
		        		openTrainCourse(currentTrainCid);
		        	}else if(currentTrainCateid == 2){//培训班
		        		openTrainClass(currentTrainCid);
		        	}
					//清空跳转cid
					currentTrainCid = null;
					currentTrainCateid = null;
			    }, 3000);
			}
	// 	}
	// }
}
//设置网页跳转app
function setupWebOpenApp(){
    api.addEventListener({
        name:'appintent'
    },function(ret,err){
        var appParam = ret.appParam;
        var url = '';
        if(api.systemType == 'ios'){
            url = ret.iosUrl;
        } else {
            url = ret.data;
        }
        var urlParam = paramWithUrl(url);
        if(urlParam.type == 'course' && urlParam.cid != 'undefined'){ //分享课程
        	currentCid = urlParam.cid;//存储当前cid
        	var categoryid = urlParam.categoryid;
        	if (categoryid != null) {
        		openCourseList(categoryid);
        	}else{
        		setupShareCourse(urlParam.cid);
        	}
        	// setupShareCourse(urlParam.cid);
	    }else if(urlParam.type == 'train'){
	    	currentCid = urlParam.cid;//存储当前cid
	    	var categid = urlParam.categid;
	    	currentTrainCateid = categid;
	    	setupTrainCourseAndClass();
	    }
    });
    //设置登录成功通知
    setupLoginSucceedNotifi();
}
function openCourseList(categoryid){
	api.openWin({useWKWebView:true,
		name : 'CourseCenter_SelectCourseListWin',
		url : 'html/CourseCenter/CourseCenter_SelectCourseListWin.html',
		pageParam : {
			sourceurl : window.location.pathname,
			sourcetitle : document.title,
			categoryid : categoryid
		},
		reload : true
	});
}
function setupShareCourse(cid){
	if ($api.getStorage('cfnetppLoginStatus') == 1) {//已登录
		$api.setStorage('cfnetShareCourseStatus', '1');//存储分享课程打开状态为已打开
		setTimeout(function(){//1秒后执行是否跳转课程
			jumpToCourse(cid);
			//清空跳转cid
			currentCid = null;
	    }, 1000);
	}
}
function openTrainCourse(cid){
	api.openWin({useWKWebView:true,
	    name: 'train-det-kc',
	    url: 'html/NewTrain/train-det-kc.html',
	    pageParam: {
	        id: cid
	    }
	});
}
function setupTrainCourseAndClass(){
	if ($api.getStorage('cfnetppLoginStatus') == 1) {//已登录
		setTimeout(function(){//1秒后执行是否跳转课程
			if (currentTrainCateid == 1) {//课程
        		openTrainCourse(currentTrainCid);
        	}else if(currentTrainCateid == 2){//培训班
        		openTrainClass(currentTrainCid);
        	}
			//清空跳转cid
			currentTrainCid = null;
			currentTrainCateid = null;
	    }, 1000);
	}
}
function openTrainClass(cid){
	api.openWin({useWKWebView:true,
	    name: 'train-det-px',
	    url: 'html/NewTrain/train-det-px.html',
	    pageParam: {
	        id: cid
	    }
	});
}
//获取当前时间戳10位
function getCurrentTime10(){
	return (Date.parse(new Date()))/1000;
}
//跳转至课程页
function jumpToCourse(cid){
	addCoursWithCid(cid);
	openCoursePage(cid);
}
function openCoursePage(cid){
	var pageParam = new Object();
	pageParam["userType"] = '0';//学生
	pageParam["courseid"] = cid;
	pageParam["planid"] = '0';
	pageParam["usertype"] = '0';//学生
	pageParam["opentype"] = 'courseCenter';//公共课程
	pageParam["openType"] = 'share';//分享课程
	api.openWin({useWKWebView:true,
	    name: 'Teacher_StudyCourseInfo',
	    url: 'html/Teacher_Teach/Teacher_StudyCourseInfo.html',
	    pageParam: pageParam
	});
}
function paramWithUrl(url){
    var Request = new Object();
    var str = url.replace('logiscfnet://', ''); //去掉协议头
    var strs = str.split("&");
    for(var i=0;i<strs.length;i++){
        Request[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
    }
    return Request;
}
function addCoursWithCid(cid){
	var param = {
		uid : $api.getStorage('cfnetppuid'),
		cid : cid,
		jid : $api.getStorage('cfnetppjxid')
	};
	cfnetppPOST(url_addCourse, param, false, function(data, status){
		if (status == 'success') {
			if (data.code == 200) {
				api.sendEvent({
				    name: 'refreshCourseListNotifi',
				    extra: {
				        cid: cid
				    }
				});
			}
		}
	});
}